/**
 * 
 */
package com.uit.project.bustrip.activity;

import java.lang.reflect.ParameterizedType;
import com.uit.project.bustrip.bointerface.BaseBO;
import com.uit.project.bustrip.factory.BoFactory;
import com.uit.project.bustrip.factory.EntityFactory;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * @author Nguyen
 * 
 */
public class GenericActivity<T, ID> extends BaseActivity {
	public static final String BUNDLE_NAME = "objectData";
	private BaseBO<T, ID> bo = null;
	private Class<?> idClazz = null;
	private Class<?> entityClazz = null;

	private T objectData = null;

	protected Button btnSave = null;
	protected Button btnNew = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.uit.project.bustrip.activity.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.init();
		this.initView();
		this.initData();
	}

	@SuppressWarnings("unchecked")
	private void init() {
		this.entityClazz = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
		this.idClazz = (Class<ID>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[1];
		BaseBO<T, ID> bo = BoFactory.instance(this.entityClazz.getSimpleName());
		bo.setContext(this);
		setBo(bo);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			T obj = (T) extras.get(BUNDLE_NAME);
			if (obj == null)
				obj = createNewObject();
			this.deSerializeEntity(obj);
		}
	}

	public void initView() {
		if (btnSave != null) {
			btnSave.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					T entity = serializeEntity();
					save(entity);
				}
			});
		}

		if (btnNew != null) {
			btnNew.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					newOnClick();
				}
			});
		}
	}
	
	public void initData() {

	}
	public void newOnClick(){
		T obj = createNewObject();
		this.deSerializeEntity(obj);
	}
	protected T serializeEntity() {
		return null;
	}

	protected void deSerializeEntity(T entity) {

	}

	@SuppressWarnings("unchecked")
	public T createNewObject() {
		return (T) EntityFactory.instance(entityClazz);
	}

	public void beforeSave(T obj) {

	}

	public void saveOnClick(T obj) {
		this.validateData(obj);
		this.beforeSave(obj);
		save(obj);
		this.afterSave(obj);
	}

	public void save(T obj) {
		int rs = this.getBo().insertOrUpdate(obj);
		if (rs < 0) {

		} else {

		}
	}

	public void validateData(T obj) {

	}

	public void afterSave(T obj) {

	}

	/**
	 * @return the idClazz
	 */
	public Class<?> getIdClazz() {
		return idClazz;
	}

	/**
	 * @return the entityClazz
	 */
	public Class<?> getEntityClazz() {
		return entityClazz;
	}

	/**
	 * @return the bo
	 */
	public BaseBO<T, ID> getBo() {
		return bo;
	}

	/**
	 * @param bo
	 *            the bo to set
	 */
	public void setBo(BaseBO<T, ID> bo) {
		this.bo = bo;
	}

	/**
	 * @return the objectData
	 */
	public T getObjectData() {
		return objectData;
	}

	/**
	 * @param objectData
	 *            the objectData to set
	 */
	public void setObjectData(T objectData) {
		this.objectData = objectData;
	}
}
